package com.lovestudying.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lovestudying.entity.Comment;
import com.lovestudying.entity.Reply;
import com.lovestudying.service.LikeListService;
import com.lovestudying.service.ReplyService;
import com.lovestudying.service.impl.LikeListServiceImpl;
import com.lovestudying.service.impl.ReplyServiceImpl;
import com.lovestudying.util.DBUtil_Pro;
import com.lovestudying.util.PageInfo;

/**
 * 回复(reply)Controller
 * 
 * @author OldDeer
 *
 */
@WebServlet("/ReplyServlet")
public class ReplyServlet extends HttpServlet {
	ReplyService replyService = new ReplyServiceImpl();
	private Gson gson = new Gson();
	LikeListService likeListService = new LikeListServiceImpl();
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReplyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String parameter = request.getParameter("op");
		switch (parameter) {
		case "getReplyToInfoBy":
			getReplyToInfoBy(request, response);
			break;
		case "postReply":
			postReply(request, response);
			break;
		case "getReplyNumByParent":
			getReplyNumByParent(request, response);
			break;

		default:
			break;
		}
	}

	private void getReplyNumByParent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String parentGroupUuid = request.getParameter("parentGroupUuid");
		Integer replyNum = replyService.getReplyNumByParent(parentGroupUuid);
		response.getWriter().print(replyNum);
		// TODO 自动生成的方法存根

	}

	private void getReplyToInfoBy(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String parentGroupUuid = request.getParameter("parentGroupUuid");
		String orderParam = request.getParameter("orderParam");
		String desc = request.getParameter("desc");
		String NowAccount = request.getParameter("NowAccount");
		Integer pageSize = null;
		Integer pageNum = null;
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		if (request.getParameter("pageNum") != null) {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		PageInfo<Reply> getCommentsByUUID = replyService.getReplyToInfoBy(parentGroupUuid, orderParam, desc, pageSize,
				pageNum);
		for (Reply element : getCommentsByUUID.getData()) {
			element.setThisAccountLike(likeListService.getCommentLikeState(NowAccount, element.getReply_id()));
		}
		String JgetCommentsByUUID = gson.toJson(getCommentsByUUID);
		response.getWriter().print(JgetCommentsByUUID);

	}

	private void postReply(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String replyId = DBUtil_Pro.GetUUID();
		String createTime = DBUtil_Pro.GetNowTime();
		String accountId = request.getParameter("accountId");
		String replyCotent = request.getParameter("Cotent");
		String replyTo = request.getParameter("replyTo");
		String parentComment = request.getParameter("parentComment");
		if (replyService.postReply(replyId, createTime, accountId, replyCotent, replyTo, parentComment)) {
			response.getWriter().print("评论成功");
		} else {
			response.getWriter().print("评论失败");
		}

	}

}
